/**
 * @Filename: uedScroll.js
 * @Description: Scroll event
 * @Version: 1.0.0
 * @Author: hanlx.si-tech
 * @UpdateBy:
 * Copyright (c) 2012-2013 si-tech
 *
 * @example
 * <div class="scroll">
 * 	<span class="scrollPrev nav-pre"></span>
 *	<span class="scrollNext nav-next"></span>
 *	<div class="navwrap">
 *		<div class="scrollWrap">
 *			<ul class="scrollList nav-list">
 *				<li class="first curr"><a href="#">南京</a></li>
 *				<li><a href="#">无锡</a></li>
 *			</ul>
 *		</div>
 *	</div>
 * </div>
 * $("div.scroll").UED_scroll();
 *
*/
(function($){
	$.fn.UED_scroll = function(option){
		var defaults = {
			time:500,                   //默认滚动时间间隔
			num:1,					    //每次滚动数目
			showNum:0,                  //显示数目
			leftBtn:".scrollPrev",		//向左滚动按钮
			rightBtn:".scrollNext",		//向右滚动按钮
			wrap:".scrollWrap",			//滚动条外层对象
			list:".scrollList",         //滚动列表
			btnVisible:true,            //当展现项目小于展现区块宽度时，默认显示左右按钮；当为false时，隐藏左右滚动按钮
			flag:0					//滚动项目数
		};
		function scrollfn(o,opts){
			var _leftBtn = o.find(opts.leftBtn),
				_rightBtn = o.find(opts.rightBtn),
				_wrap = o.find(opts.wrap),
				_list = o.find(opts.list);
			var flag = opts.flag,
				num = opts.num,
				time = opts.time,
				btnVisible = opts.btnVisible,
				showNum = opts.showNum;
			var litem = _list.children();
			var ln = _list.children().length;
			//判断是否有展现项目个数限制，如果有的话，通过项目数设置展示区宽度
			if(!litem.hasClass("curr")){
				litem.first().addClass("curr");	
			}
			if(showNum>0){
				var wrapW = 0;
				for(var i=0;i<showNum;i++){
					wrapW += litem.eq(i).outerWidth(true);
				}
				if($.browser.msie && $.browser.version=="9.0"){
						wrapW += 5+3*(showNum-2);
				}
				_wrap.width(wrapW);
			}else{
				_wrap.css("width","100%");
				var wrapW = _wrap.width();
			}
			var listW = 0;
			var listmgl = 0;
			for(var i=0;i<flag;i++){
				listmgl += litem.eq(i).outerWidth(true);
			}
			
			for(var i=0;i<ln;i++){
				listW += litem.eq(i).outerWidth(true);				
			}
			if($.browser.msie && $.browser.version=="9.0"){
				listW += 5+3*(showNum-2);
			}
			_list.css({"width":listW+"px"});
			
			
			if(listW<=wrapW){
				if(btnVisible){
					_leftBtn.css({cursor:"default", opacity:"0.4"});
					_rightBtn.css({cursor:"default", opacity:"0.4"});
				}else{
					_leftBtn.add(_rightBtn).css("display","none");
				}
			}else{
				_leftBtn.css({cursor:"default", opacity:"0.4"});
				_rightBtn.css({cursor:"pointer", opacity:"1"});
			}
			
			//点击左侧滚动条，显示区展现项目向右侧移动
			_leftBtn.click(function(){
				_rightBtn.css({cursor:"pointer", opacity:"1"});
				flag -= num;
				if(flag < 0){
					flag = 0;
					return false;
				}else{
					for(var i=flag;i<flag+num;i++){
						listmgl -= litem.eq(i).outerWidth(true);
					}
					if(flag == 0){
						_leftBtn.css({cursor:"default", opacity:"0.4"});
					}
					_list.animate({"margin-left": -listmgl+"px"},time);
					if(showNum>0){
						wrapW = 0;
						for(var i=flag;i<flag+showNum;i++){
							wrapW += litem.eq(i).outerWidth(true); 
						}
						_wrap.width(wrapW);
					}
				}
				return false;
			});
			//点击右侧滚动条，显示区展现项目向左侧移动
			_rightBtn.click(function(){
				_leftBtn.css({cursor:"pointer", opacity:"1"});
				if(showNum>0){
					if(flag > ln - showNum){
						_rightBtn.css({cursor:"default", opacity:"0.4"});
						flag = ln - showNum;
						return false;	
					}else{
						if(flag == ln - showNum){
							_rightBtn.css({cursor:"default", opacity:"0.4"});
							return false;
						}
						flag = eval(flag+num);
						for(var i=flag-num;i<flag;i++){
							listmgl += litem.eq(i).outerWidth(true);
						}
						_list.animate({"margin-left": -listmgl+"px"},time);
						wrapW = 0;
						for(var i=flag;i<eval(flag+showNum);i++){
							wrapW += litem.eq(i).outerWidth(true);
						}
						_wrap.width(wrapW);
					}
				}else{
					if(flag > ln-num){
						_rightBtn.css({cursor:"default", opacity:"0.4"});
						flag = ln-num;
						return false;
					}else{
						var diff = eval(listW - listmgl);
						if(diff<=wrapW){
							_rightBtn.css({cursor:"default", opacity:"0.4"});
							return false;
						}else{
							flag += num;
							for(var i=flag-num;i<flag;i++){
								listmgl += litem.eq(i).outerWidth(true);
							}
							_list.animate({"margin-left": -listmgl+"px"},time);
						}
					}
					return false;
				}
			});
		}
		
		return this.each(function(i,n){
			var _this = $(n);
			var opts = $.extend(true,{},defaults,option);
			_this.data("option",opts);
			var p = _this.data("option");
			scrollfn(_this,p);
        })
	}
	
	/*$(function(){
        $("div.scrollpanel").UED_scroll();
    })*/
})(jQuery);